package com.yangzehan.workbench.Controller;

import com.yangzehan.domaim.User;
import com.yangzehan.utils.DateTimeUtil;
import com.yangzehan.utils.UUIDUtil;
import com.yangzehan.vo.pagevo;
import com.yangzehan.workbench.domain.Activity;
import com.yangzehan.workbench.domain.Clue;
import com.yangzehan.workbench.domain.Tran;
import com.yangzehan.workbench.service.ClueService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ClueController {
    @Resource
    private ClueService service;
    @RequestMapping(value = "/getUser.do")
    @ResponseBody
    public List<User>getUser(){
    List<User>list=service.getuser();
    return list;
    }
    @RequestMapping(value = "/saveClue.do")
    @ResponseBody
    public boolean save(HttpServletRequest request,Clue clue) {
        clue.setCreateBy(((User) request.getSession().getAttribute("user")).getName());
        String CreateTime = DateTimeUtil.getSysTime();
        String id = UUIDUtil.getUUID();
        clue.setId(id);
        clue.setCreateTime(CreateTime);
        System.out.println(clue);
        boolean n = service.save(clue);
        return n;
    }
    @RequestMapping(value = "/pagefindclue.do")
    @ResponseBody
    public pagevo<Clue> pagefindclue(HttpServletRequest request, Clue clue) {
        String pageNo = request.getParameter("pageNo");
        int pageno = Integer.valueOf(pageNo);
        String pageSize = request.getParameter("pageSize");
        Integer pagesize = Integer.valueOf(pageSize);
        Integer pagNo = (pageno - 1) * pagesize;
        Map<String, Object> map = new HashMap();
        map.put("clue", clue);
        map.put("pageNo", pagNo);
        map.put("pageSize", pagesize);
        pagevo<Clue> vo = service.page(map);
        return vo;
    }
    @RequestMapping(value = "/cluedetail.do")
    @ResponseBody
    public ModelAndView cluedetail(Clue clue) {
        ModelAndView modelAndView = new ModelAndView();
        clue = service.findClue(clue);
        modelAndView.addObject("clue", clue);
        modelAndView.setViewName("forward:workbench/clue/detail.jsp");
        return modelAndView;
    }
    @RequestMapping(value = "/RefreshNotes.do")
    @ResponseBody
    public List<Activity> RefreshNotes(HttpServletRequest request) {
        String id = request.getParameter("id");
        List<Activity> list = service.getaById(id);
        return list;
    }
    @RequestMapping(value = "/deleteClue.do")
    @ResponseBody
    public boolean deleteClue(HttpServletRequest request) {
        String id = request.getParameter("id");
        boolean n;
        n = service.deleteClue(id);
        return n;
    }
    @RequestMapping(value = "/get.do")
    @ResponseBody
    public List<Activity> get(HttpServletRequest request) {
        String id = request.getParameter("id");
        String aname = request.getParameter("name");
        Map<String, String> map = new HashMap<>();
        map.put("aname", aname);
        map.put("id", id);

        List<Activity> list = service.getByaname(map);
        return list;
    }
    @RequestMapping(value = "/guanlian.do")
    @ResponseBody
    public boolean guanlian(HttpServletRequest request){

     String cid=request.getParameter("cid");
     String aid[]= request.getParameterValues("aid");
        System.out.println(cid);
        System.out.println(aid);
     boolean n=service.guanlian(cid,aid);

        return n;
    }
    @RequestMapping(value = "/searchByname.do")
    @ResponseBody
    public List<Activity>searchByname(HttpServletRequest request){
String name= request.getParameter("name");
 List<Activity> list=service.getByname(name);
 return list;
    }
    @RequestMapping(value = "/convert.do")
    @ResponseBody
    public ModelAndView convert(HttpServletRequest request ,Tran tran){
        ModelAndView modelAndView = new ModelAndView();
            String clueId=request.getParameter("clueId");
        System.out.println(clueId);
            String falg =request.getParameter("flag");
        System.out.println(falg);
            tran.setCreateTime(DateTimeUtil.getSysTime());
            tran.setCreateBy(((User)request.getSession().getAttribute("user")).getName());
            tran.setId(UUIDUtil.getUUID());
        System.out.println(tran);

           boolean n= service.zhuanghuan(tran,falg,clueId);
            modelAndView.addObject("flag",n);
        System.out.println(n);

            modelAndView.setViewName("forward:workbench/clue/index.jsp");


        return modelAndView;
    }

}
